﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Business;

namespace ReportForSCC
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            task = Authenticated.Instance;
            listFlights = new List<Flight>();
            try
            {
                // prod
                signature = task.Login("p8api", "ABCDEabcde!6789", "DEF");

                //test
                //signature = task.Login("p8quanth", "QTHqth@1987", "DEF");
            }
            catch
            {

            }
            
        }

        private List<Flight> listFlights;
        private Authenticated task;
        private string signature=string.Empty;


        private void Form1_Load(object sender, EventArgs e)
        {
            cmbFrom.SelectedIndex = 0;
            cmbTo.SelectedIndex = 0;   
        }

        private void btnShow_Click(object sender, EventArgs e)
        {

            this.FlightBindingSource.DataSource = null;
            this.reportViewer1.RefreshReport();

            listFlights.Clear();
            try
            {
                if (cmbFrom.SelectedIndex == 0 && cmbTo.SelectedIndex == 0)
                {
                    
                    foreach (string def in cmbFrom.Items)
                    {
                        foreach (string arr in cmbTo.Items)
                        {
                            if (def == "ALL" || arr == "ALL")
                                continue;
                            listFlights.AddRange(Business.GetFlightInfomation.GetFlightStatus(def, arr, dateTimePicker1.Value, dateTimePicker2.Value, signature));
                        }
                    }
                }
                else
                {
                    if (cmbFrom.SelectedIndex != 0 && cmbTo.SelectedIndex != 0)
                    {
                        listFlights = Business.GetFlightInfomation.GetFlightStatus(cmbFrom.SelectedItem.ToString(), cmbTo.SelectedItem.ToString(), dateTimePicker1.Value, dateTimePicker2.Value, signature);
                    }
                    if (cmbFrom.SelectedIndex == 0 && cmbTo.SelectedIndex != 0)
                    {
                        foreach (string def in cmbFrom.Items)
                        {
                            if (def == "ALL")
                                continue;
                            listFlights = Business.GetFlightInfomation.GetFlightStatus(def, cmbTo.SelectedItem.ToString(), dateTimePicker1.Value, dateTimePicker2.Value, signature);
                        }
                    }
                    if (cmbFrom.SelectedIndex != 0 && cmbTo.SelectedIndex == 0)
                    {
                        foreach (string arr in cmbTo.Items)
                        {
                            if (arr == "ALL")
                                continue;
                            listFlights = Business.GetFlightInfomation.GetFlightStatus(cmbFrom.SelectedItem.ToString(),arr, dateTimePicker1.Value, dateTimePicker2.Value, signature);
                        }
                    }
                    
                }

                this.FlightBindingSource.DataSource = listFlights;
                this.reportViewer1.RefreshReport();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            /*
            this.FlightBindingSource.DataSource = null;
            this.reportViewer1.RefreshReport();
            try
            {
                listFlights = Business.GetFlightInfomation.GetFlightStatus(cmbFrom.SelectedItem.ToString(), cmbTo.SelectedItem.ToString(), dateTimePicker1.Value, dateTimePicker2.Value, signature);
                
                this.FlightBindingSource.DataSource = listFlights;
                this.reportViewer1.RefreshReport();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show( ex.Message,"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            */
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            task.Logout(signature);
        }

        
    }
}